package com.zishi.venuesbooking.ac;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.zishi.venuesbooking.BaseActivity;
import com.zishi.venuesbooking.R;
import com.zishi.venuesbooking.adapter.CommentAdapter;
import com.zishi.venuesbooking.entity.Comment;

import org.json.JSONException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class CommentsActivity extends BaseActivity {

    @Override
    protected int initLayout() {
        return R.layout.activity_comments;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() throws JSONException, IOException {

    }

    private Button btnPostComment;
    private RecyclerView rvComments;
    private CommentAdapter adapter;
    private List<Comment> comments = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comments);

        btnPostComment = findViewById(R.id.btn_post_comment);
        rvComments = findViewById(R.id.rv_comments);

        // 设置评论列表
        adapter = new CommentAdapter(comments);
        rvComments.setLayoutManager(new LinearLayoutManager(this));
        rvComments.setAdapter(adapter);

        // 加载评论数据
        loadComments();

        // 点击发布评论按钮
        btnPostComment.setOnClickListener(v -> showCommentDialog());
    }

    private void loadComments() {
        // 模拟数据加载
        List<Comment> comments = new ArrayList<>();
        comments.add(new Comment(
                "1", "user1", "https://avatar.com/user1.jpg", "用户1",
                "venue1", "测试场馆1", "这个场馆很不错，设施齐全", 5, System.currentTimeMillis()
        ));
        comments.add(new Comment(
                "2", "user2", "https://avatar.com/user2.jpg", "用户2",
                "venue1", "测试场馆1", "一般般，价格有点贵", 3, System.currentTimeMillis() - 100000
        ));

        adapter.updateData(comments);
        // 从网络或数据库加载评论数据
        // comments.addAll(...);
        // adapter.notifyDataSetChanged();
    }

    private void showCommentDialog() {
        BottomSheetDialog dialog = new BottomSheetDialog(this);
        View dialogView = getLayoutInflater().inflate(R.layout.dialog_post_comment, null);
        dialog.setContentView(dialogView);

        // 初始化对话框中的视图
        ImageView[] stars = {
                dialogView.findViewById(R.id.iv_rate_star1),
                // 初始化其他4个星星...
        };
        TextView tvRatingValue = dialogView.findViewById(R.id.tv_rating_value);
        EditText etComment = dialogView.findViewById(R.id.et_comment);
        Button btnClose = dialogView.findViewById(R.id.btn_close);
        Button btnRelease = dialogView.findViewById(R.id.btn_release);

        // 星星点击事件
        for (int i = 0; i < stars.length; i++) {
            final int position = i;
            stars[i].setOnClickListener(v -> {
                updateStars(stars, position + 1);
                tvRatingValue.setText(String.valueOf(position + 1));
            });
        }

        // 关闭按钮
        btnClose.setOnClickListener(v -> dialog.dismiss());

        // 发布按钮
        btnRelease.setOnClickListener(v -> {
            String comment = etComment.getText().toString().trim();
            int rating = Integer.parseInt(tvRatingValue.getText().toString());

            if (comment.isEmpty()) {
                Toast.makeText(this, "请输入评论内容", Toast.LENGTH_SHORT).show();
                return;
            }

            postComment(comment, rating);
            dialog.dismiss();
        });

        dialog.show();
    }

    private void updateStars(ImageView[] stars, int rating) {
        for (int i = 0; i < stars.length; i++) {
            stars[i].setImageResource(i < rating ?
                    R.drawable.ic_star_filled :
                    R.drawable.ic_star_outline);
        }
    }

    private void postComment(String comment, int rating) {
        // 发布评论到服务器
        // 成功后刷新评论列表
    }
}